package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.entity.AdObject;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.HashMap;

public class AdWebViewFragment extends BaseFragment {
	
	private static AdWebViewFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.webview)
	private WebView webview;

	private static AdObject adObject;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_ad_webview, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.ad_wevbiew));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		getWebViewData();
	}

	private void getWebViewData() {
		if (adObject == null) return;

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("id", adObject.getId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_ADS_DETAIL, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						webview.getSettings().setDefaultTextEncodingName("UTF-8");
						webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
						webview.getSettings().setUseWideViewPort(true);
						webview.getSettings().setLoadWithOverviewMode(true);
						responseInfo.result = responseInfo.result.replaceAll("\\<img ", "\\<img width='100%' ");
						webview.loadData(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), "text/html; charset=UTF-8", null);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	public static void setAdObject(AdObject adObject) {
		AdWebViewFragment.adObject = adObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static AdWebViewFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
